//
//  ClassCourseDetailViewController.m
//  HTP
//
//  Created by admin on 15/11/4.
//  Copyright © 2015年 nit. All rights reserved.
//

#import "ClassCourseDetailViewController.h"
#import "EvaluationViewController.h"
@interface ClassCourseDetailViewController ()
@property (nonatomic,strong) UIScrollView *backView;

@property (nonatomic,strong) UIImageView  *stateImg;

@property (nonatomic,strong) UILabel      *subLabel;

@property (nonatomic,strong) UILabel      *countLabel;

@property (nonatomic,retain) UILabel      *addressLaebl;

@property (nonatomic,strong) UILabel      *dateLaebl;

@property (nonatomic,strong) UILabel      *startTimeLaebl;

@property (nonatomic,strong) UILabel      *endTimeLaebl;

@property (nonatomic,strong) UIImageView  *teacherImg;

@property (nonatomic,strong) UILabel      *teacherName;

@property (nonatomic,strong) UILabel      *teacherZj;

@property (nonatomic,retain) UIView       *eveView;

@property (nonatomic,retain) UILabel      *eveLabel;

@property (nonatomic,strong) UIImageView  *line_1;

@property (nonatomic,retain) UIImageView  *line_2;

@property (nonatomic,retain) UILabel      *stuEve;

@end

@implementation ClassCourseDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.backView];
    [self _initView];
    // Do any additional setup after loading the view.
}
-(void)viewWillAppear:(BOOL)animated{
    [self _initNav];
    
}
- (void)viewDidAppear:(BOOL)animated{
    [self.backView setContentSize:CGSizeMake(ScreenWidth, ScreenHeihgt - NavHe)];
    [self setClassData];
}

//TODO:UI绘制
-(void)_initView{
    UILabel *courseDetailLbl = [self getNormolLabel];
    courseDetailLbl.textAlignment = _TEXT_ALIGN_LEFT;
    courseDetailLbl.text = @"课程详情";
    courseDetailLbl.font = UIFONTSYS(20);
    [self.backView addSubview:courseDetailLbl];
    
    UIScrollView *superView = (UIScrollView *)self.backView;
    [courseDetailLbl makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(superView).offset(10);
        make.top.equalTo(superView).offset(15);
    }];
    
    _stateImg = [[UIImageView alloc] init];
    _stateImg.image = [UIImage imageNamed:@"weipingjia"];
    [self.backView addSubview:_stateImg];
    [_stateImg makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(superView).offset(ScreenWidth - 80);
        make.right.equalTo(superView).offset(-10);
        make.top.equalTo(superView).offset(18);
    }];
    
    UIImageView *lineImg = [[UIImageView alloc] init];
    lineImg.image = [UIImage imageNamed:@"line_h"];
    [self.backView addSubview:lineImg];
    
    [lineImg makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(superView).offset(10);
        make.width.equalTo(ScreenWidth - 20);
        make.top.equalTo(courseDetailLbl.bottom).offset(10);
        make.height.equalTo(1);
    }];
    
    //教师部分
    _teacherImg = [[UIImageView alloc] init];
    [_teacherImg setBackgroundColor:[UIColor redColor]];
    [self.backView addSubview:_teacherImg];
    [_teacherImg makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lineImg.bottom).offset(15);
        make.width.equalTo(60);
        make.right.equalTo(lineImg).offset(-20);
        make.height.equalTo(60);
    }];
    
    _teacherName = [self getNormolLabel];
    _teacherName.text = @"教师: 刘思思";
    _teacherName.textAlignment = _TEXT_ALIGN_CENTER;
    _teacherName.font = UIFONTSYS(16);
    [self.backView addSubview:_teacherName];
    [_teacherName makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(_teacherImg.centerX);
        make.top.equalTo(_teacherImg.bottom).offset(5);
    }];
    
    //课程详情部分
    _subLabel = [self getNormolLabel];
    _subLabel.text = @"学科: 英语";
    _subLabel.textAlignment = _TEXT_ALIGN_LEFT;
    [self.backView addSubview:_subLabel];
    [_subLabel makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(lineImg);
        make.top.equalTo(lineImg.bottom).offset(15);
        make.right.equalTo(_teacherName.left).offset(-5);
    }];
    
    _countLabel = [self getNormolLabel];
    _countLabel.text = @"次数: 5/10";
    _countLabel.textAlignment = _TEXT_ALIGN_LEFT;
    [self.backView addSubview:_countLabel];
    [_countLabel makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(lineImg);
        make.top.equalTo(_subLabel.bottom).offset(10);
        make.right.equalTo(_teacherName.left).offset(-5);
    }];
    
    _addressLaebl = [self getNormolLabel];
    _addressLaebl.text = @"地点: 家里家里家里家里家家里家家里家家里家";
    _addressLaebl.textAlignment = _TEXT_ALIGN_LEFT;
    _addressLaebl.numberOfLines = 0;
    [self.backView addSubview:_addressLaebl];
    [_addressLaebl makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(lineImg);
        make.top.equalTo(_countLabel.bottom).offset(10);
        make.right.equalTo(_teacherName.left).offset(-5);
    }];
    
    _dateLaebl = [self getNormolLabel];
    _dateLaebl.text = @"时间: 2015-10-29";
    _dateLaebl.textAlignment = _TEXT_ALIGN_LEFT;
    _dateLaebl.numberOfLines = 0;
    [self.backView addSubview:_dateLaebl];
    [_dateLaebl makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(lineImg);
        make.top.equalTo(_addressLaebl.bottom).offset(10);
        make.right.equalTo(_teacherName.left).offset(-5);
    }];
    
    _startTimeLaebl = [self getNormolLabel];
    _startTimeLaebl.text = @"上课开始时间: 18:00";
    _startTimeLaebl.textAlignment = _TEXT_ALIGN_LEFT;
    _startTimeLaebl.numberOfLines = 0;
    [self.backView addSubview:_startTimeLaebl];
    [_startTimeLaebl makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(lineImg);
        make.top.equalTo(_dateLaebl.bottom).offset(10);
        make.right.equalTo(superView).offset(-5);
    }];
    
    _endTimeLaebl = [self getNormolLabel];
    _endTimeLaebl.text = @"上课结束时间: 20:00";
    _endTimeLaebl.textAlignment = _TEXT_ALIGN_LEFT;
    _endTimeLaebl.numberOfLines = 0;
    [self.backView addSubview:_endTimeLaebl];
    [_endTimeLaebl makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(lineImg);
        make.top.equalTo(_startTimeLaebl.bottom).offset(10);
        make.right.equalTo(superView).offset(-5);
    }];
    
    //教师总结
    _line_1 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"line_h"]];
    [self.backView addSubview:_line_1];
    [_line_1 makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(10);
        make.right.equalTo(lineImg);
        make.top.equalTo(_endTimeLaebl.bottom).offset(20);
        make.height.equalTo(1);
    }];
    
    _teacherZj = [self getNormolLabel];
    _teacherZj.text = @"上课结束时间: 20:00";
    _teacherZj.textAlignment = _TEXT_ALIGN_LEFT;
    _teacherZj.numberOfLines = 0;
    [self.backView addSubview:_teacherZj];
    [_teacherZj makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(lineImg);
        make.top.equalTo(_endTimeLaebl.bottom).offset(30);
        make.right.equalTo(lineImg);
    }];
    
    _line_2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"line_h"]];
    [self.backView addSubview:_line_2];
    [_line_2 makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(10);
        make.right.equalTo(lineImg);
        make.top.equalTo(_teacherZj.bottom).offset(10);
        make.height.equalTo(1);
    }];
    //学社评价
    
    _eveView = [[UIView alloc] init];
    [self.backView addSubview:_eveView];
    [_eveView makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.backView).offset(10);
        make.right.equalTo(self.backView).offset(-10);
        make.top.equalTo(_teacherZj.bottom).offset(20);
        make.height.equalTo(25);
    }];
    
    _stuEve = [self getNormolLabel];
    _stuEve.textAlignment = _TEXT_ALIGN_LEFT;
    [self.backView addSubview:_stuEve];
    [_stuEve makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(lineImg);
        make.top.equalTo(_eveView.bottom).offset(10);
        make.right.equalTo(lineImg);
    }];
}
/**
 *  自定义导航栏
 yunhe
 */
-(void)_initNav{
    UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]
                                       initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
                                       target:nil action:nil];
    negativeSpacer.width = -5;
    UIView *viw = [self creatVieWidthImage:SYSTEMBACKIMAGE imageSel:SYSTEMBACKIMAGE title:@"我的课程" observer:self action:@selector(backToQM)];
    self.navigationItem.leftBarButtonItems = @[negativeSpacer,[[UIBarButtonItem alloc] initWithCustomView:viw]];
    if (_courseState == NoneEvaluation) {
        [self addRightButtonWithTitle:@"评价" withAction:@selector(rightButton)];
    }
    
}
//TODO:重写get方法
-(UIScrollView *)backView{
    if (!_backView) {
        _backView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeihgt - NavHe)];
        [_backView setBackgroundColor:[UIColor clearColor]];
        _backView.userInteractionEnabled = YES;
    }
    return _backView;
}
//TODO:Action
/**
 *  评价按钮Action
 */
-(void)rightButton{
    [self showSVProgressSuccess:@"我要去评价"];
    EvaluationViewController *evaluationVc = [[EvaluationViewController alloc] init];
    [self.navigationController pushViewController:evaluationVc animated:YES];
}
/**
 *  获取数据后 设置数据
 */
-(void)setClassData{
    _subLabel.text = @"学科: 计算机科学";
    
    _countLabel.text = @"次数: 5/10";
    
    _addressLaebl.text = @"地点: 浙江大学宁波理工学院";
    
    _dateLaebl.text = @"时间: 2015-12-11";
    
    _startTimeLaebl.text = @"上课开始时间: 18:10";
    
    _endTimeLaebl.text = @"上课结束时间: 20:00";
    
//    [_teacherZj setText:@"教师总结: 这集客这集客这集客这集客这集客这集客这集客这集客这集客这集客这集客这集客这集客这集客这集客这集客这集客这集客这集客这集客这集客这集客这集客这集客这集客这集客这集客这集客这集客这集客这集客这集客这集客这集客这集客这集客这集客这集客这集客这集客这集客这集客这集客"];
    
    _teacherZj.text = @"教师总结: 这集客这集客这集客这集客这集客这集客这集客这集客这集客这集客这集客这集客这集客这集客这集客这集客这集客这集客这集客这集客这集客这集客这集客这集客这集客这集客这集客这集客这集客这集客这这集客这集客这集客这集客这集客这集客这集客这集客这集客这集客这集客这集客这集客这集客这集客这集客这集客这集客这集客这集客这集客这集客这集客这集客这集客这集客这集客这集客这集客这集客这集客这集客这集客这集客这集客这集客这集客这集客这集客这集客这集客这集客这集客这集客这集客这集客这集客这集客这集客这集客这集客这集客这集客这集客这集客这集客这集客这集客这集客这集客集客这集客这集客这集客这集客这集客这集客这集客这集客这集客这集客这集客这集客";
    _stuEve.text = @"评价内容: 很好 不错 我喜欢这个青苗家教";
    switch (_courseState) {
        case NoneEvaluation:
            [self setNoneEve];
            break;
            case NoneStart:
            [self setNoneStart];
            break;
            case Finish:
            [self setFinish];
            break;
        default:
            break;
    }
    [self.view layoutIfNeeded];
    [self.view layoutSubviews];
    [self.backView layoutSubviews];
    [self.backView layoutIfNeeded];
    __block CGFloat maxY = 0.0;
    [self.backView.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (CGRectGetMaxY(obj.frame) > maxY) {
            maxY = CGRectGetMaxY(obj.frame);
        }
    }];
    DLog(@"%lf  ",maxY);
    [self.backView setContentSize:CGSizeMake(ScreenWidth, (maxY + 10) > (ScreenHeihgt - NavHe + 5)?(maxY + 10):(ScreenHeihgt-NavHe + 5))];
    DLog(@"%lf   ",self.backView.contentSize.height);
}
//TODO:设置页面不同状态UI
/**
 *  设置详情页为未完成
 */
-(void)setNoneStart{
    _stateImg.image = [UIImage imageNamed:@"weiwancheng"];
    if (_teacherZj.superview || _line_2.superview || _line_1.superview || _eveView.superview || _stuEve.superview) {
        [_teacherZj removeFromSuperview];
        [_line_1 removeFromSuperview];
        [_line_2 removeFromSuperview];
        [_eveView removeFromSuperview];
        [_stuEve removeFromSuperview];
    }
}
/**
 *  设置页面为未评价
 */
-(void)setNoneEve{
    _stateImg.image = [UIImage imageNamed:@"weipingjia"];
    if (_eveView.superview || _stuEve.superview) {
        [_eveView removeFromSuperview];
        [_stuEve removeFromSuperview];
    }
}
/**
 *  设置页面为完成页面
 */
-(void)setFinish{
     _stateImg.image = [UIImage imageNamed:@"yiwancheng"];
    [self initEveView:_eveView];
}
/**
 *  绘制等级view 、eveView
 *
 *  @param eveView 
 */
-(void)initEveView:(UIView *)eveView{
    [eveView.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [obj removeFromSuperview];
    }];
    
    UILabel *text = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
    text.text = @"评价等级: ";
    text.font = [UIFont systemFontOfSize:15];
    CGSize size = [text.text textSizeWithFont:[UIFont systemFontOfSize:15] constrainedSize:CGSizeMake(ScreenWidth, 20)];
    [text setFrame:CGRectMake(0, 0, size.width, size.height)];
    [eveView addSubview:text];
    NSMutableArray *xxArrays = [NSMutableArray array];
    for (int i = 0 ; i < 5; i++) {
        UIButton *xxBtn = [[UIButton alloc] initWithFrame:CGRectMake(size.width+5 +(25) * i, 0, 20, 20)];
        [xxBtn setBackgroundImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
        [xxBtn setBackgroundImage:[UIImage imageNamed:@"star"] forState:UIControlStateSelected];
        xxBtn.tag = i;
        [eveView addSubview:xxBtn];
        [xxArrays addObject:xxBtn];
    }
    [xxArrays enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UIButton *btn = (UIButton *)obj;
        if (btn.tag <= 3) {
            btn.selected = YES;
        }else{
            btn.selected = NO;
        }
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
